﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MillerApp.DataContract;
using System.Threading;
using MillerApp.DataContract.Common;
using MillerApp.DataAccess.MillerDB;
using MillerApp.Common;
using MillerApp.UserControls;
using MillerApp.DataContract.MillerDB;
using MillerApp.Business.Rules.Common;
using MillerApp.CommonLib.Win32;

namespace MillerApp.Stock
{
    /// <summary>
    /// 登录窗体
    /// </summary>
    public partial class Login : SubFormBase
    {
        public Login()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 当前用户
        /// </summary>
        public UserEntity CurrentUser { get; set; }

        /// <summary>
        /// 点击登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            //创建log文件夹，存储日志
            string directory = System.AppDomain.CurrentDomain.BaseDirectory;
            string logdirectory = System.IO.Path.Combine(directory, "log");
            if (System.IO.Directory.Exists(logdirectory) == false)
            {
                System.IO.Directory.CreateDirectory(logdirectory);
            }

            //判断登陆是否成功
            string userid = this.txtUserName.Text;
            string passwd = this.txtPassword.Text;

            string msg=string.Empty;
            UserEntity user=null;
            bool loginOK = MillerApp.Stock.Common.Common.Login(userid, passwd, out msg, out user);
            if (loginOK)//如果登录成功，则关闭登录窗口开启主页面
            {
                CurrentUser = user;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                MessageBox.Show(msg);
            }
        }

        private void Login_Load(object sender, EventArgs e)
        {
            WinEffect.SetMid(this);
            Win32Helper.AnimateWindow(this.Handle, 500, Win32Helper.AnimateWindowFlags.AW_CENTER);
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(null, null);
            }
        }
    }
}
